//
//  PasswordVerifyController.m
//  RuiHeApp
//
//  Created by Jun on 2022/12/5.
//

#import "PasswordVerifyController.h"
#import "RHWindowTipManage.h"
#import "TextInputBoxView.h"
#import "TextInputModel.h"

@interface PasswordVerifyController ()

@property (nonatomic ,strong) TextInputBoxView *inputBox;

@property (nonatomic ,strong) TextInputModel *inputModel;

@end

@implementation PasswordVerifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    _inputModel = [[TextInputModel alloc] init];
    [_inputModel setTitile:@""];
    [_inputModel setPlaceholder:@"请输入密码".rh_localized];
    [_inputModel setInputType:TextInputPasswordType];
    [_inputModel setIsSecure:YES];
    _inputModel.minLen = 8;
    _inputModel.maxLen = 16;
    [self setupUI];
}

- (void)setupUI{
    
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *boxView = [[UIView alloc] init];
    [boxView setBackgroundColor:UIColor.whiteColor];
    [boxView.layer setCornerRadius:9];
    [boxView shadow:UIColor.blackColor opacity:0.1 radius:5 offset:CGSizeMake(0, 1)];
    [self.view addSubview:boxView];
    
    QMUILabel *lab = [[QMUILabel alloc] init];
    [lab setTextAlignment:NSTextAlignmentLeft];
    [lab setFont:[UIFont boldSystemFontOfSize:15]];
    [lab setTextColor:FontColor(@"#000000")];
    [lab setText:@"密码".rh_localized];
    [boxView addSubview:lab];
    
    _inputBox = [[TextInputBoxView alloc] init];
    [_inputBox setModel:self.inputModel];
    [boxView addSubview:_inputBox];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundColor:CMainColorGreen];
    [btn setTitle:@"确认".rh_localized forState:UIControlStateNormal];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn.layer setCornerRadius:10];
    [boxView addSubview:btn];
    
    [boxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.centerY.mas_offset(-20);
    }];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(30);
        make.top.mas_offset(15);
    }];
    
    [_inputBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lab);
        make.top.equalTo(lab.mas_bottom).offset(20);
        make.height.mas_equalTo(45);
    }];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.centerX.mas_offset(0);
        make.top.equalTo(self.inputBox.mas_bottom).offset(20);
        make.bottom.mas_offset(-15);
    }];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    

    kWeakSelf;
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf hidden];
    }];
    
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        if (weakSelf.inputModel.content.length == 0) {
            [ProgressHUDManage showInfo:weakSelf.inputModel.placeholder];
            return;
        }
        
        if (weakSelf.callBackBlock) {
            weakSelf.callBackBlock(weakSelf.inputModel.content);
        }
        
        [weakSelf hidden];
    }];
    

}

- (void)hidden{
    [[RHWindowTipManage shareWindowTipManage] hidden];
}
@end
